<template>
	<view>
		<view style="width: 750rpx;height: 88rpx;line-height: 88rpx;background-color: #fff;">
			<u-tabs :list="list" active-color='rgb(48, 49, 51)' :isSame='true' inactive-color='rgb(96, 98, 102)'
				:is-scroll="false" :current="current" @change="change"></u-tabs>
		</view>
		<view v-if="lists.length!=0">
			<view style="margin-top: 20rpx;" v-for="(item,index) in lists" :key="index" @click="toCouponDetail(item)">
				<view class="content">
					<view class="tag_contain">{{item.discountType==1?'满减券':'折扣券'}}</view>
					<view class="name_reduce">
						<text style="padding-top: 10rpx;color:#333;font-size:32rpx">{{item.name}}</text>
						<view style="position: absolute;right: 0rpx;">
							<text class="data_dw" v-if="item.discountType === 1">￥</text>
							<text
								class="data_rq">{{item.discountType === 1? fen2yuan(item.discountPrice): item.discountPercent / 10.0}}</text>
							<text class="data_dw" v-if="item.discountType === 2">折</text>
						</view>
					</view>
					<view class="date_price">
						<text v-if="item.validityType === 2">有效期：领取后 {{ item.fixedEndTerm }} 天内可用</text>
						<text v-else> 有效期: {{timeFormat(item.validStartTime, 'yyyy-mm-dd') }}至{{ timeFormat(item.validEndTime, 'yyyy-mm-dd') }}</text>
						<text> 满 {{ fen2yuan(item.usePrice) }} 可用 </text>
					</view>
				</view>
				<view class="desc">
					<text>{{ item.description }}</text>
					<view :class="current==0||current==1?'ligth':'dark'" @click.stop="getCoupon(item)">
						{{(current==0&&item.canTake)?'立即领取':(current==0&&!item.canTake)?'已领取':current==1?'去使用':current==2?'已使用':current==3?'已失效':''}}
					</view>
				</view>
			</view>
		</view>
		<view style="margin-top: 200rpx;" v-else>
			<u-empty text="暂无优惠券" mode="list" icon="list"></u-empty>
		</view>

		<!-- <u-loadmore :status="status" /> -->
	</view>
</template>

<script>
	var that = ''
	import {
		timeFormat
	} from '../../unit/utils.js'
	export default {
		data() {
			return {
				list: [{
					name: '领券中心'
				}, {
					name: '已领取'
				}, {
					name: '已使用',
				}, {
					name: '已失效',
				}],
				current: 0,
				status: 'loadmore',
				page: 0,
				lists: [],
				obj: {
					"pageNo": 1,
					"pageSize": 10,
					"channel":'Channel_ZFB',
					"mobile": "",
					"status": 1
				}
			}
		},
		onLoad() {
			that = this

		},
		onShow() {
			if(uni.getStorageSync('phone')){
				this.lists = []
				if (this.current == 0) {
					this.getCouponTemplateListMethod()
				} else {
					this.lists = []
					this.page = 1
					this.obj.status = this.current
					this.obj.pageNo = 1
					this.couponPageMethod()
				}
			}
		},
		methods: {
			timeFormat,
			fen2yuan(price) {
				return (price / 100.0).toFixed(2);
			},
			getCoupon(e) {
				if (this.current == 0) {
					if (!uni.getStorageSync('phone')) {
						uni.navigateTo({
							url: '/pages/index/authorization'
						})
					} else {
						this.$u.api.takeCoupon({
							"mobile": uni.getStorageSync('phone'),
							"channel":'Channel_ZFB',
							"templateId": e.id //数量
						}).then(res => {
							if (res) {
								uni.showToast({
									title: '领取成功',
									icon: 'none',
									duration: 2000
								})
								this.getCouponTemplateListMethod()
							}
						})
					}
				}else if(this.current == 1){
					uni.navigateTo({
						url: '/pages/index/coupondetail?id=' + e.id+'&index=1'
					})
				}
			},
			couponPageMethod() {
				this.$u.api.couponPage(this.obj).then(res => {
					//分页
					if (res.data.list.length < 10) {
						this.status = 'nomore'
					} else {
						this.status = 'loadmore'
					}
					this.page = this.page + 1
					this.lists = this.lists.concat(res.data.list)
					this.obj.pageNo = this.page
				})
			},
			getCouponTemplateListMethod() {
				this.$u.api.getCouponTemplateList({
					"mobile": uni.getStorageSync('phone'),
					"channel":'Channel_ZFB',
					"count": 100 //数量
				}).then(res => {
					that.lists = res.data
				})
			},
			change(index) {
				this.current = index;
				this.obj.mobile = uni.getStorageSync('phone')
				if (index != 0) {
					this.lists = []
					this.page = 1
					this.obj.status = index
					this.obj.pageNo = 1
					this.couponPageMethod()
				} else {
					this.getCouponTemplateListMethod()
				}
			},
			toCouponDetail(e) {
				if (this.current == 0) {
					uni.navigateTo({
						url: '/pages/index/coupondetail?id=' + e.id+'&index=2'
					})
					
				} else {
					uni.navigateTo({
						url: '/pages/index/coupondetail?id=' + e.id+'&index=1'
					})
				}

			}
		}
	}
</script>
<style>
	page {
		background-color: #f6f6f6;
	}
</style>
<style lang="scss" scoped>
	.data_dw {
		color: #ff0000;
		font-size: 28rpx;
		margin-bottom: 15rpx;
	}

	.data_rq {
		color: #ff0000;
		font-size: 50rpx;
	}

	.content {
		width: 710rpx;
		margin-left: 20rpx;
		background: #fff;
		border-radius: 20rpx 20rpx 0 0;
		-webkit-mask: radial-gradient(circle at 12rpx 100%, #0000 12rpx, red 0) -12rpx;
		box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.04);
		position: relative;
		padding: 20rpx;
		border-bottom: 2rpx dashed #d3d3d3;

		.tag_contain {
			width: 100rpx;
			color: #fff;
			height: 40rpx;
			line-height: 40rpx;
			text-align: center;
			font-size: 24rpx;
			position: absolute;
			top: 0rpx;
			left: 0rpx;
			border-radius: 20rpx 0 20rpx 0;
			background: linear-gradient(90deg, #FF3607, #FFA18B);
		}

		.name_reduce {
			width: 670rpx;
			display: flex;
			align-items: center;
			margin-top: 30rpx;
			position: relative;
		}





		.date_price {
			display: flex;
			width: 670rpx;
			justify-content: space-between;
			align-items: center;
			color: #666;
			margin-top: 30rpx;
		}
	}

	.desc {
		width: 710rpx;
		margin-left: 20rpx;
		background: #fff;
		-webkit-mask: radial-gradient(circle at 12rpx 0%, #0000 12rpx, red 0) -12rpx;
		box-shadow: rgba(#000, 0.1);
		box-sizing: border-box;
		padding: 24rpx 30rpx;
		box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.04);
		border-radius: 0 0 20rpx 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		text {
			color: #666;
			font-size: 24rpx;
		}

		view {
			width: 160rpx;
			height: 60rpx;
			background: linear-gradient(90deg, #FF3607, #FFA18B);
			border-radius: 30rpx;
			color: #fff;
			font-size: 24rpx;
			line-height: 60rpx;
			text-align: center;
		}

		.ligth {
			background: linear-gradient(90deg, #FF3607, #FFA18B);
			color: #fff;
		}

		.dark {
			background: #999;
			color: #fff;
		}
	}
</style>